import Message from 'tdesign-miniprogram/message/index';

Page({
  data: {
    // 用户信息
    userInfo: {
      userId: '1',
      userName: '书虫小明',
      avatar: '/static/avatar1.png',
      title: '文学青年',
      follows: 23,
      followers: 156,
      bookCount: 12,
      badges: [
        {
          badgeId: '1',
          name: '读书新手',
          category: 'literature',
          image: '/static/home/card0.png',
          obtained: true
        },
        {
          badgeId: '2',
          name: '文学爱好者',
          category: 'literature',
          image: '/static/home/card1.png',
          obtained: true
        },
        {
          badgeId: '3',
          name: '科幻探索者',
          category: 'scifi',
          image: '/static/home/card2.png',
          obtained: true
        },
        {
          badgeId: '4',
          name: '历史学者',
          category: 'history',
          image: '/static/home/card3.png',
          obtained: true
        },
        {
          badgeId: '5',
          name: '哲学思考者',
          category: 'philosophy',
          image: '/static/home/card4.png',
          obtained: true
        },
        {
          badgeId: '6',
          name: '诗歌爱好者',
          category: 'poetry',
          image: '/static/home/card0.png',
          obtained: true
        },
        {
          badgeId: '7',
          name: '连续阅读',
          category: 'literature',
          image: '/static/home/card1.png',
          obtained: true
        },
        {
          badgeId: '8',
          name: '深度思考',
          category: 'philosophy',
          image: '/static/home/card2.png',
          obtained: false
        }
      ],
      bookList: [
        {
          bookId: '1',
          title: '活着',
          cover: '/static/home/card0.png'
        },
        {
          bookId: '2',
          title: '追风筝的人',
          cover: '/static/home/card1.png'
        },
        {
          bookId: '3',
          title: '三体',
          cover: '/static/home/card2.png'
        },
        {
          bookId: '4',
          title: '百年孤独',
          cover: '/static/home/card3.png'
        },
        {
          bookId: '5',
          title: '人类简史',
          cover: '/static/home/card4.png'
        }
      ]
    },
    // 当前互动标签
    currentInteractionTab: 'likes',
    // 互动数据
    interactionData: {
      likes: [
        {
          id: '1',
          title: '《活着》书摘',
          description: '人是为活着本身而活着，而不是为了活着之外的任何事物而活着。',
          avatar: '/static/home/card0.png',
          time: '2小时前'
        },
        {
          id: '2',
          title: '《追风筝的人》书摘',
          description: '为你，千千万万遍。',
          avatar: '/static/home/card1.png',
          time: '1天前'
        },
        {
          id: '3',
          title: '《三体》书摘',
          description: '给岁月以文明，而不是给文明以岁月。',
          avatar: '/static/home/card2.png',
          time: '3天前'
        }
      ],
      comments: [
        {
          id: '1',
          title: '《百年孤独》书摘',
          description: '评论：这本书让我对魔幻现实主义有了新的理解',
          avatar: '/static/home/card3.png',
          time: '1小时前'
        },
        {
          id: '2',
          title: '《人类简史》书摘',
          description: '评论：作者的观点很有启发性',
          avatar: '/static/home/card4.png',
          time: '2天前'
        }
      ],
      collections: [
        {
          id: '1',
          title: '《小王子》书摘',
          description: '收藏了这条书摘',
          avatar: '/static/home/card0.png',
          time: '1天前'
        },
        {
          id: '2',
          title: '《红楼梦》书摘',
          description: '收藏了这条书摘',
          avatar: '/static/home/card1.png',
          time: '2天前'
        }
      ]
    }
  },

  onLoad() {
    this.loadUserInfo();
  },

  onShow() {
    // 页面显示时刷新数据
    this.refreshData();
  },

  /**
   * 加载用户信息
   */
  async loadUserInfo() {
    try {
      // 这里应该调用真实的 API
      // 目前使用模拟数据
      console.log('加载用户信息成功');
    } catch (error) {
      console.error('加载用户信息失败:', error);
      Message.error({
        context: this,
        content: '加载失败，请重试',
        duration: 2000
      });
    }
  },

  /**
   * 刷新数据
   */
  async refreshData() {
    await this.loadUserInfo();
  },

  /**
   * 获取当前互动数据
   */
  get currentInteractionData() {
    return this.data.interactionData[this.data.currentInteractionTab] || [];
  },

  /**
   * 切换互动标签
   */
  switchInteractionTab(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({ currentInteractionTab: type });
  },

  /**
   * 更换头像
   */
  changeAvatar() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        // 这里应该上传头像到服务器
        this.setData({
          'userInfo.avatar': tempFilePath
        });
        Message.success({
          context: this,
          content: '头像更新成功',
          duration: 2000
        });
      }
    });
  },

  /**
   * 显示称号升级规则
   */
  showTitleRules() {
    wx.showModal({
      title: '称号升级规则',
      content: '通过阅读不同类型的书籍、发布书摘、获得点赞等方式可以提升称号等级。\n\n当前称号：文学青年\n下一级：文学大师\n需要：阅读10本文学类书籍',
      showCancel: false
    });
  },

  /**
   * 查看关注列表
   */
  viewFollows() {
    wx.navigateTo({
      url: '/pages/follows/index?type=follows'
    });
  },

  /**
   * 查看粉丝列表
   */
  viewFollowers() {
    wx.navigateTo({
      url: '/pages/follows/index?type=followers'
    });
  },

  /**
   * 查看书单
   */
  viewBookList() {
    wx.navigateTo({
      url: '/pages/booklist/index'
    });
  },

  /**
   * 查看全部书籍
   */
  viewAllBooks() {
    wx.navigateTo({
      url: '/pages/booklist/index'
    });
  },

  /**
   * 查看书籍详情
   */
  viewBookDetail(e) {
    const bookId = e.currentTarget.dataset.bookId;
    wx.navigateTo({
      url: `/pages/book-detail/index?id=${bookId}`
    });
  },

  /**
   * 显示徽章详情
   */
  showBadgeDetail(e) {
    const badge = e.currentTarget.dataset.badge;
    wx.showModal({
      title: badge.name,
      content: `徽章类型：${this.getCategoryName(badge.category)}\n获取条件：${this.getBadgeRequirement(badge.badgeId)}\n${badge.obtained ? '已获得' : '未获得'}`,
      showCancel: false
    });
  },

  /**
   * 显示徽章特效
   */
  showBadgeEffect(e) {
    const badge = e.currentTarget.dataset.badge;
    if (badge.obtained) {
      // 这里可以添加徽章特效动画
      Message.success({
        context: this,
        content: `炫耀 ${badge.name} 徽章！`,
        duration: 2000
      });
    }
  },

  /**
   * 查看互动详情
   */
  viewInteractionDetail(e) {
    const item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/post-detail/index?id=${item.id}`
    });
  },

  /**
   * 查看帮助中心
   */
  viewHelp() {
    wx.navigateTo({
      url: '/pages/help/index'
    });
  },

  /**
   * 查看意见反馈
   */
  viewFeedback() {
    wx.navigateTo({
      url: '/pages/feedback/index'
    });
  },

  /**
   * 查看关于我们
   */
  viewAbout() {
    wx.navigateTo({
      url: '/pages/about/index'
    });
  },

  /**
   * 退出登录
   */
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除用户数据
          wx.clearStorageSync();
          // 跳转到登录页
          wx.reLaunch({
            url: '/pages/login/index'
          });
        }
      }
    });
  },

  /**
   * 获取分类名称
   */
  getCategoryName(category) {
    const categoryMap = {
      literature: '文学',
      scifi: '科幻',
      history: '历史',
      philosophy: '哲学',
      poetry: '诗歌'
    };
    return categoryMap[category] || '其他';
  },

  /**
   * 获取徽章获取条件
   */
  getBadgeRequirement(badgeId) {
    const requirementMap = {
      '1': '完成第一本书的阅读',
      '2': '阅读3本文学类书籍',
      '3': '阅读2本科幻类书籍',
      '4': '阅读2本历史类书籍',
      '5': '阅读2本哲学类书籍',
      '6': '阅读2本诗歌类书籍',
      '7': '连续阅读7天',
      '8': '发布10条深度思考的书摘'
    };
    return requirementMap[badgeId] || '完成特定任务';
  },

  /**
   * 下拉刷新
   */
  async onPullDownRefresh() {
    await this.refreshData();
    wx.stopPullDownRefresh();
  }
});
